package com.linkage.seapeak.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;

public class BaseWhiteActivity extends BaseActivity {

    TextView titleView;
    ViewStub stub;
    LinearLayout actionLayout;
    View backView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarMode(false);
        super.setContentView(R.layout.activity_base_white);
        initBase();
    }

    private void initBase(){
        backView = findViewById(R.id.icon_back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backEvent();
            }
        });
        titleView = findViewById(R.id.activity_title);
        actionLayout = findViewById(R.id.action_layout);
        stub = findViewById(R.id.content_view);
    }

    @Override
    public void setContentView(int layoutResID) {
        stub.setLayoutResource(layoutResID);
        stub.inflate();
    }

    @Override
    public void onBackPressed() {
        backEvent();
    }

    /**
     * 结束事件重写必须品
     */
    public void backEvent() {
        finish();
    }

    /**
     * 包含标题的初始化
     * @param layoutResID
     * @param title
     */
    public void setContentView(int layoutResID, String title) {
        setContentView(layoutResID);
        setTitle(title);
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        titleView.setText(title);
        TAG = title.toString();
    }

    /**
     * 添加右上角事件
     * @param action
     */
    public View addMenu(MenuAction action) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        View view = null;
        if (!TextUtils.isEmpty(action.menuText)) {
            view = new TextView(this);
            ((TextView) view).setText(action.menuText);
            ((TextView) view).setTextColor(0xff289f9f);
            ((TextView) view).setTextSize(14);
            view.setPadding(dp2px(10), 0, dp2px(10), 0);
            ((TextView) view).setMinWidth(dp2px(45));
            ((TextView) view).setGravity(Gravity.CENTER);
        } else if (action.menuIconRes != 0) {
            view = new ImageView(this);
            ((ImageView) view).setScaleType(ImageView.ScaleType.FIT_XY);
            view.setPadding(dp2px(10), dp2px(10), dp2px(10), dp2px(10));
            view.setMinimumWidth(dp2px(45));
            ((ImageView) view).setImageResource(action.menuIconRes);
        } else
            return view;

        view.setOnClickListener(action.clickListener);
        actionLayout.addView(view, params);
        return view;
    }
}
